package com.farewell.blog.controller.admin;

import com.baomidou.mybatisplus.plugins.Page;
import com.farewell.blog.entity.QueryBean;
import com.farewell.blog.entity.SearchBlog;
import com.farewell.blog.entity.SearchResource;
import com.farewell.blog.pojo.Blog;
import com.farewell.blog.pojo.Resource;
import com.farewell.blog.pojo.ResourceList;
import com.farewell.blog.service.ResourceListService;
import com.farewell.blog.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author Farewell
 * @date 2021/3/4
 */
@Controller
@RequestMapping("/admin")
public class ResourceController {
    @Autowired
    private ResourceListService resourceListService;

    @Autowired
    private ResourceService resourceService;

    @PostMapping("/resources/save")
    public String saveBlog(ResourceList resourceList, RedirectAttributes redirectAttributes) {
        try {
            if (resourceList.getCheckId() != null) {
                // 非空代表是修改操作
                resourceListService.updateResourceList(resourceList);
                redirectAttributes.addFlashAttribute("message", "修改成功");
            }else {
                resourceListService.addResourceList(resourceList);
                redirectAttributes.addFlashAttribute("message", "添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute("message", "操作失败");
        }
        return "redirect:/admin/blogs";
    }

    @GetMapping("/resources/input")
    public String input(Model model) {
        model.addAttribute("resources", resourceService.getAllResource());
        model.addAttribute("resourceList", new ResourceList());
        return "admin/resources-input";
    }

    @GetMapping("/resources")
    public String resources(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        QueryBean queryBean = new QueryBean(pageNum, 10, null);
        Page<ResourceList> pageData = resourceListService.listResourceList(queryBean);
        model.addAttribute("pageData", pageData);
        model.addAttribute("resources", resourceService.getAllResource());
        return "admin/resources";
    }

    @PostMapping("/resources/search")
    public String search(SearchResource searchResource, Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        QueryBean queryBean = new QueryBean(pageNum, 10, null);
        Page<ResourceList> pageData = resourceListService.listResourceListByCondition(searchResource, queryBean);
        model.addAttribute("pageData", pageData);
        return "admin/resources :: resourceListList";
    }
}
